'use strict';
import React from "react";
import BaseStore from "./BaseStore";
import {action, computed, observable, runInAction, toJS} from "mobx";

class MineStore extends BaseStore {

    constructor(params) {
        super(params);
        this.myPublish = {
            wait: [],
            through: [],
        };
    }

    @observable myPublish;

    @computed
    get getMyPublish() {
        return toJS(this.myPublish);
    }

    @action
    removePublishItem = (type, index) => {
        let dataSourceTemp = {...this.myPublish};
        runInAction(() => {
            dataSourceTemp[type].splice(index, 1);
            this.myPublish = dataSourceTemp;
        });
    };


    @action
    requestMyPublish = async (type, data) => {
        let audit_status = 'wait';
        if (type === 0) { //审核中
            audit_status = 'wait';
        } else if (type === 1) { //通过审核
            audit_status = 'through';
        }
        const dataSource = await this.postRequest(ServicesApi.INFORMATION_QUERY, data);
        runInAction(() => {
            if (dataSource.code === StatusCode.SUCCESS_CODE) {
                if (data.page === 1) {
                    if (type === 0) {
                        this.myPublish['wait'] = dataSource.data;
                    } else {
                        this.myPublish['through'] = dataSource.data;
                    }
                } else {
                    let temp;
                    if (type === 0) {
                        temp = this.myPublish['wait'];
                    } else {
                        temp = this.myPublish['through'];
                    }
                    if (dataSource.data.length !== 0) {
                        if (type === 0) {
                            this.myPublish['wait'] = temp.concat(dataSource.data);
                        } else {
                            this.myPublish['through'] = temp.concat(dataSource.data);
                        }
                    }
                }
            } else {
                if (type === 0) {
                    this.myPublish['wait'] = [];
                } else {
                    this.myPublish['through'] = [];
                }
            }
        });
        return dataSource
    };
}


export default MineStore